////////////////////////////////////////////////////////////////////////////////////////////////////
// file:	Forms.cs
//
// summary:	Implements the forms class
////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.IO;
using System.Data;
using System.Collections;
using Dimok.Data;
using AFU = AFORMAUTLib;

////////////////////////////////////////////////////////////////////////////////////////////////////
// namespace: Dimok.Documents
//
// summary:	.
////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Dimok.Documents
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Exception for signalling pdf conversion errors. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class PDFConversionException:Exception {

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Constructor. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="msg">	The message. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public PDFConversionException(string msg):base(msg){}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Forms. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class Forms
	{
		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">		. </param>
		/// <param name="dataRow">	The data row. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] FillPDFForm(byte[] fileIn,DataRow dataRow)
		{
			return FillPDFForm(FileManager.GetFileFromBytes(fileIn),dataRow,true);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">		. </param>
		/// <param name="dataRow">	The data row. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] FillPDFForm(string fileIn,DataRow dataRow)
		{
			return FillPDFForm(fileIn,dataRow,false);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">				. </param>
		/// <param name="dataRow">			The data row. </param>
		/// <param name="deleteFileIn">	. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] FillPDFForm(string fileIn,DataRow dataRow,bool deleteFileIn)
		{
			return FileManager.GetBytesFromFile(FillPDFForm(fileIn,null,dataRow,deleteFileIn),true);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">		. </param>
		/// <param name="fileOut">	if empty - automaticaly generate an output file name. </param>
		/// <param name="dataRow">	The data row. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string FillPDFForm(byte[] fileIn,string fileOut,DataRow dataRow)
		{
			return FillPDFForm(FileManager.GetFileFromBytes(fileIn),fileOut,dataRow,true);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">		. </param>
		/// <param name="fileOut">	if empty - automaticaly generate an output file name. </param>
		/// <param name="dataRow">	The data row. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string FillPDFForm(string fileIn,string fileOut,DataRow dataRow)
		{
			return FillPDFForm(fileIn,fileOut,dataRow,false);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">				. </param>
		/// <param name="fileOut">			if empty - automaticaly generate an output file name. </param>
		/// <param name="dataRow">			The data row. </param>
		/// <param name="deleteFileIn">	. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string FillPDFForm(string fileIn,string fileOut,DataRow dataRow,bool deleteFileIn)
		{
			Hashtable fields = new Hashtable();
			DataColumnCollection cols = dataRow.Table.Columns;
			foreach( DataColumn col in cols )
				fields.Add(col.ColumnName,dataRow[col.ColumnName].ToString());
			return FillPDFForm(fileIn,fileOut,fields,deleteFileIn);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">	. </param>
		/// <param name="fields">	. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] FillPDFForm(byte[] fileIn,Hashtable fields)
		{
			string fileTemp = Dimok.FileManager.GetFileFromBytes(fileIn);
			string filePDF = fileTemp.Replace(".tmp",".pdf");
			File.Move(fileTemp,filePDF);
			return FillPDFForm(filePDF,fields,true);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">	. </param>
		/// <param name="fields">	. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] FillPDFForm(string fileIn,Hashtable fields)
		{
			return FillPDFForm(fileIn,fields,false);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">				. </param>
		/// <param name="fields">				. </param>
		/// <param name="deleteFileIn">	. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] FillPDFForm(string fileIn,Hashtable fields,bool deleteFileIn)
		{
			return FileManager.GetBytesFromFile(FillPDFForm(fileIn,null,fields,deleteFileIn),true);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">		. </param>
		/// <param name="fileOut">	if empty - automaticaly generate an output file name. </param>
		/// <param name="fields">		. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string FillPDFForm(byte[] fileIn,string fileOut,Hashtable fields)
		{
			return FillPDFForm(FileManager.GetFileFromBytes(fileIn),fileOut,fields,true);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">		. </param>
		/// <param name="fileOut">	if empty - automaticaly generate an output file name. </param>
		/// <param name="fields">		. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string FillPDFForm(string fileIn,string fileOut,Hashtable fields)
		{
			return FillPDFForm(fileIn,fileOut,fields,false);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fill form and save it to file. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="fileIn">				. </param>
		/// <param name="fileOut">			if empty - automaticaly generate an output file name. </param>
		/// <param name="fields">				. </param>
		/// <param name="deleteFileIn">	. </param>
		///
		/// <returns>	name of output file. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string FillPDFForm(string fileIn,string fileOut,Hashtable fields,bool deleteFileIn)
		{
			if( fileOut == null || fileOut == "" )fileOut = Path.GetTempFileName();
			Acrobat.CAcroAVDoc avDoc = null;
			try {
				Type oAVDoc = Type.GetTypeFromProgID("AcroExch.AVDoc",true);
				avDoc = (Acrobat.CAcroAVDoc)Activator.CreateInstance(oAVDoc,true);
				avDoc.Open(fileIn,"");
				Type oAForm = Type.GetTypeFromProgID("AFormAut.App",true);
				Acrobat.CAcroPDDoc pdDoc = (Acrobat.CAcroPDDoc)avDoc.GetPDDoc();
				AFU.AFormApp aForm = (AFU.AFormApp)Activator.CreateInstance(oAForm,true);
				//DAF.AFormClass daf = new DimokAForms.AFormClass();
				//avDoc = daf.GetAVDoc(fileIn);
				AFU.Fields aFields = (AFU.Fields)aForm.Fields;
				foreach( AFU.Field aField in aFields)
					if( fields.ContainsKey(aField.Name) )
						aField.Value = fields[aField.Name].ToString();
				//Acrobat.CAcroPDDoc pdDoc = (Acrobat.CAcroPDDoc)avDoc.GetPDDoc();
				pdDoc.Save((short)Acrobat.PDSaveFlags.PDSaveFull,fileOut);
				pdDoc.Close();
				if( deleteFileIn )File.Delete(fileIn);
				return fileOut;
			}finally{
				avDoc.Close(1);
			}
		}
	}
}